<script>
import {formatTime} from "../../utils/formatTime.js";
import {del, add, all} from '@/api/carousel.js'

export default {
  name: "carousel",
  methods: {
    formatTime,
    refresh() {
      all().then(res => {
        if (res.code === 200) {
          this.carouselData = res.data
        } else {
          this.$message.error("获取数据失败")
        }
      })
    },
    addCarousel(data) {
      add(data).then(res => {
        if (res.code === 200) {
          this.$message.success("添加成功")
          this.refresh()
          this.addCarouselDialogVisible = false
          this.addCarouselFormData = {}
        } else {
          this.$message.error("添加失败")
        }
      })
    },
    handleDelete(id) {
      del(id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.refresh()
        } else {
          this.$message.error("删除失败")
        }
      })
    }
  },
  data() {
    return {
      addCarouselDialogVisible: false,
      editCarouselDialogVisible: false,
      editCarouselFormData: {},
      addCarouselFormData: {},
      carouselData: []
    }
  }
  ,
  created() {
    this.refresh()
  }
}
</script>

<template>
  <div id="carousel-title">
    轮播图管理
  </div>
  <div id="carousel-box">
    <div id="carousel-form">
      <el-button type="primary" disabled>增加检索</el-button>
      <el-button type="primary" @click="addCarouselDialogVisible = true">新增数据</el-button>
      <el-button type="primary">导入数据</el-button>
      <el-button type="primary">导出数据</el-button>
      <el-button type="primary" @click="refresh">刷新</el-button>
    </div>

    <div>
      <el-table id="answer-data" :data="carouselData">
        <el-table-column type="selection" width="50px"/>
        <el-table-column label="轮播图" prop="img" width="400">
          <template #default="scope">
            <img :src="scope.row.img" style="width: 100px; height: 100px;" alt="轮播图">
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="400">
          <template #default="scope">
            <span>{{ formatTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" prop="updateTime" width="400">
          <template #default="scope">
            <span>{{ formatTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button @click="handleEdit(scope.row)" type="primary" disabled>编辑</el-button>
            <el-button @click="handleDelete(scope.row.id)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <!-- 添加表单 -->
  <div>
    <el-dialog title="新增轮播图" v-model="addCarouselDialogVisible">
      <el-form :model="addCarouselFormData">
        <el-form-item label="轮播图地址">
          <el-input v-model="addCarouselFormData.img"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="addCarouselDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addCarousel(addCarouselFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<style scoped>
#carousel-box {
  background-color: #68a273;
  width: 90%;
  height: 90%;
  margin-top: 2%;
  margin-left: 3%;
  border-radius: 30px;
}

#carousel-title {
  font-size: 30px;
  margin-left: 20px;
}

#carousel-form {
  display: flex;
  padding-top: 40px;
  justify-content: flex-end;
  margin-right: 80px;
  margin-bottom: 10px;
}
</style>